const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

// pages/foodIndex/foodIndex.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner:[],
    goodsList:[],
    storeList:[],
    tag:"",
    page: 1,
    limit: 20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.removeStorage({
      key: 'tag',
    })

    this.getIndex();
  },
  getIndex(){
    let that = this;
    util.request(api.IndexUrl,{
      tag:that.data.tag
    }).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          banner: res.data.banner,
          goodsList: res.data.goodsList,
          storeList: res.data.storeList
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    wx.getStorage({
      key: 'tag',
      success (res) {
        that.setData({
          tag: res.data
        });
        that.getStoreListByTag();
      }
    });
  },
  changeTag(event){
    let tag = event.currentTarget.dataset.tag;
    this.setData({
      tag: tag
    });
    this.getStoreListByTag();
  },
  getStoreListByTag:function(){
    let that = this;
    util.request(api.StoreList, {
      tag:that.data.tag,
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          storeList: res.data.list,
        });
        //滑动到storeList
        wx.pageScrollTo({
          selector:"#storeList",
          duration: 1000
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})